package com.happystreet.config
{
	import com.happystreet.GlobalGameConstants;
	import com.happystreet.utils.CustomUtils;

	public class TextConfig
	{
		public static var languageName:String;
		private static var textEntryTable:Array;
		private static var defaultTextEntry:TextEntry;
		public function TextConfig()
		{
		}
		
		public static function setTextContent(config:String):void
		{
			var text_config:XML;
			var lang_code:String;
			var text_content:XML;
			text_config = new XML(config);
			lang_code = (GameConfig.userLanguage) ? GameConfig.userLanguage : GlobalGameConstants.DEFAULT_USER_LANGUAGE;
			languageName = text_config.config.language.(@langCode == lang_code).@countryName;
			text_content = XML(text_config.contentLanguage.(@langCode == lang_code));
			processTextEntries(text_content);
		}
		
		private static function processTextEntries(xml_text_entries:XML):void
		{
			var xml_text_entry:XML;
			var text_entry:TextEntry;
			textEntryTable = new Array();
			defaultTextEntry = new TextEntry(xml_text_entries.text.(@id == "DefaultText")[0]);
			for each (xml_text_entry in xml_text_entries..text)
			{
				text_entry = new TextEntry(xml_text_entry);
				textEntryTable.push(text_entry);
			}
		}
		
		public static function getDynamicValuesIndexes(_arg1:String, ... _args):Array
		{
			var _local6:String;
			var _local3:String = getTextEntry(_arg1).value;
			_local3 = _local3.replace(/\\n/g, "\n");
			var _local4:Array = new Array();
			var _local5:int;
			while (_local5 < _args.length)
			{
				_local6 = (("{" + _local5) + "}");
				if (_local3.indexOf(_local6) >= 0)
				{
					_local4.push([_local3.indexOf(_local6), _args[_local5].length]);
					_local3 = CustomUtils.replaceText(_local3, _local6, _args[_local5]);
				};
				_local5++;
			};
			return (_local4);
		}
		
		public static function getText(str:String, ... args):String
		{
			var _local5:String;
			var _local3:String = getTextEntry(str).value;
			_local3 = _local3.replace(/\\n/g, "\n");
			_local3 = CustomUtils.replaceGameObjectReferencesIn(_local3);
			var _local4:int;
			while (_local4 < str.length)
			{
				_local5 = (("{" + _local4) + "}");
				if (_local3.indexOf(_local5) >= 0)
				{
					_local3 = CustomUtils.replaceText(_local3, _local5, str[_local4]);
				}
				_local4++;
			}
			return _local3;			
		}
		
		
		private static function getTextEntry(_arg1:String):TextEntry
		{
			var _local2:TextEntry;
			for each (_local2 in textEntryTable)
			{
				if (_local2.id == _arg1)
				{
					return (_local2);
				}
			}
			return defaultTextEntry;
		}
	}
}